package com.fc.mall.service.impl;

import com.fc.mall.dao.UmsRolePermissionRelationDao;
import com.fc.mall.dto.UmsRoleParam;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.UmsAdminRoleRelationMapper;
import com.fc.mall.mapper.UmsRoleMapper;
import com.fc.mall.mapper.UmsRolePermissionRelationMapper;
import com.fc.mall.model.*;
import com.fc.mall.model.*;
import com.fc.mall.service.UmsRoleService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台角色管理Service实现类
 * Created by dobuy on 2018/9/30.
 */
@Service
public class UmsRoleServiceImpl implements UmsRoleService {
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRolePermissionRelationMapper rolePermissionRelationMapper;
    @Autowired
    private UmsRolePermissionRelationDao rolePermissionRelationDao;

    @Autowired
    private UmsAdminRoleRelationMapper adminRoleRelationMapper;

    @Autowired
    private MapperFacade mapperFacade;
    @Override
    public int create(UmsRoleParam role) {
        //查询是否存在重名,重名不可插入
        UmsRoleExample roleExample = new UmsRoleExample();
        roleExample.createCriteria().andNameEqualTo(role.getName());
        List<UmsRole> roleList = roleMapper.selectByExample(roleExample);
        if (roleList.size()>0){
            throw new ServiceException("该角色名称已经存在");
        }

        UmsRole umsRole = new UmsRole();
        mapperFacade.map(role, umsRole);
        umsRole.setCreateTime(new Date());
        umsRole.setStatus(1);
        umsRole.setAdminCount(0);
        umsRole.setSort(0);
        int count = roleMapper.insertSelective(umsRole);
        updatePermission(umsRole.getId(), role.getPermissionIds());
        return count;
    }

    @Override
    public int update(Long id, UmsRoleParam role) {
        if (!StringUtils.isEmpty(role.getName())){
            //查询是否存在重名,重名不可插入
            UmsRoleExample roleExample = new UmsRoleExample();
            roleExample.createCriteria().andNameEqualTo(role.getName()).andIdNotEqualTo(id);
            List<UmsRole> roleList = roleMapper.selectByExample(roleExample);
            if (roleList.size()>0){
                throw new ServiceException("该角色名称已经存在");
            }
        }
        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        mapperFacade.map(role, umsRole);
        updatePermission(id, role.getPermissionIds());
        return roleMapper.updateByPrimaryKey(umsRole);
    }

    @Override
    public int delete(List<Long> ids) {
        UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();

        adminRoleRelationExample.createCriteria().andRoleIdIn(ids);

        int count = adminRoleRelationMapper.countByExample(adminRoleRelationExample);
        if(count > 0) {
            throw new ServiceException("用户已设置当前角色。不可删除");
        }
        UmsRolePermissionRelationExample example=new UmsRolePermissionRelationExample();
        example.createCriteria().andRoleIdIn(ids);
        rolePermissionRelationMapper.deleteByExample(example);
        UmsRoleExample roleExample = new UmsRoleExample();
        roleExample.createCriteria().andIdIn(ids);
        return roleMapper.deleteByExample(roleExample);
    }

    @Override
    public List<UmsPermission> getPermissionList(Long roleId) {
        return rolePermissionRelationDao.getPermissionList(roleId);
    }

    @Override
    public int updatePermission(Long roleId, List<Long> permissionIds) {
        //先删除原有关系
        UmsRolePermissionRelationExample example=new UmsRolePermissionRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        rolePermissionRelationMapper.deleteByExample(example);
        //批量插入新关系
        List<UmsRolePermissionRelation> relationList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            UmsRolePermissionRelation relation = new UmsRolePermissionRelation();
            relation.setRoleId(roleId);
            relation.setPermissionId(permissionId);
            relationList.add(relation);
        }
        return rolePermissionRelationDao.insertList(relationList);
    }

    @Override
    public List<UmsRole> list() {
        return roleMapper.selectByExample(new UmsRoleExample());
    }

    @Override
    public UmsRoleParam getById(Long roleId) {
        UmsRole role = roleMapper.selectByPrimaryKey(roleId);
        List<UmsPermission> umsPermissions = getPermissionList(roleId);
        UmsRoleParam roleParam = new UmsRoleParam();
        mapperFacade.map(role, roleParam);
        umsPermissions.stream().forEach(umsPermission -> {
            if(null !=umsPermission) {
                roleParam.getPermissionIds().add(umsPermission.getId());
            }
        });
        return roleParam;
    }
}
